<template>
	<view class="fillOrderWrapper" v-if="show">
		<view class="infoBox">
			<view class="title">基本信息</view>
			<view class="info">
				<view class="name">零件编号：</view>
				<view class="value">{{info.materialCode}}</view>
			</view>
			<view class="info">
				<view class="name">填报人：</view>
				<view class="value">{{info.operationEmployeeName}}</view>
			</view>
			<view class="info">
				<view class="name">日期：</view>
				<view class="value">{{getDate(info.recordTime)}}</view>
			</view>

			<!-- <view class="info">
				<view class="name">设备名称：</view>
				<view class="value">{{info.industrialEquipmentName}}</view>
			</view>
			<view class="info">检点时间：{{info.recordTime}}</view> -->
		</view>

		<view class="infoBox">
			<view class="title">表单内容</view>
			<!-- <view class="infoBottom">
				<view class="yg">操作员工：{{info.operationEmployeeName}}</view>
				<view class="opt">检点部门：{{info.dept}}</view>
				<view class="gw">工位：{{info.workstationSection}}</view>
			</view> -->
			<view class="info">
				<text class="name">审核状态：</text>
				<text class="value">{{getStatusText(info.status)}}</text>
			</view>
			<view class="info">
				<text class="name">备注：</text>
				<text class="value">{{info.remark}}</text>
			</view>
			<view class="itemWRapper">
				<view :class="['comItemBox',isHaveNoChildShow(e)?'nochild':'']" v-for="(e,i) in htmlRes" :key="i">
					<view class="comItem" :style="{width:item.span*100/24 + '%'}" v-if='isShowItem(item)'
						v-for="(item,index) in e" :key="index">
						<text class="title_label" v-if='item.showLabel'><text v-if='item.required'
								style="color: red;">*</text>{{item.label}}</text>
						<text v-if='item.tag=="row-title"' class="row-title">{{item.titleText}}</text>
						<text v-if='item.tag=="el-date-picker"' class="row-title">{{item.defaultValue}}</text>
						<text v-if='item.tag=="el-select"' class="row-title">{{getSelectValue(item)}}</text>
						<u-radio-group :disabled='true' v-if='item.tag=="el-radio-group"' v-model="item.defaultValue"
							@change="radioGroupChange(item.vModel)">
							<u-radio v-for="(opt, opti) in item.slot.options" :key="opti" :name="opt.value">
								{{opt.label}}
							</u-radio>
						</u-radio-group>
						<u-input :disabled='true' class="el-input" v-if="item.tag=='el-input'"
							v-model="item.defaultValue" :type="item.tagIcon" :border="true"
							:placeholder='item.placeholder'></u-input>
						<view class="file" v-if="item.tag=='el-upload'">
							<view class="imgBox" v-if="item.defaultValue">
								<image @click="clickImg(item.defaultValue,imgIndex)" class="img" :src="img" mode=""
									v-for="(img,imgIndex) in item.defaultValue.split(',')" :key="imgIndex"></image>
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				typeList: [],
				contentJosn: null,
				info: null,
				show: false,
				htmlRes: []
			}
		},
		onLoad(e) {
			uni.showLoading({
				title: '加载中'
			});
			uni.setNavigationBarTitle({
				title: e.name
			})
			const par = {
				openid: uni.getStorageSync('openid'),
				param: `{"id":${e.id}}`
			}
			this.$u.api.getOrderInfo(par).then(res => {
				const {
					data
				} = res
				this.contentJosn = JSON.parse(data.content)
				this.getHtmlInfo()
				console.log(this.contentJosn);
				this.info = data
				this.show = true

				uni.hideLoading()
			})
		},
		mounted() {},
		methods: {
			getSelectValue(item) {

				if (item.multiple) {
					const opt = item.slot.options.filter(e => item.defaultValue.includes(e.value))
					if (opt && opt.length > 1) {
						const arr = opt.map(e => e.label)
						return arr.join(',')
					} else {
						return ''
					}
				} else {
					const opt = item.slot.options.filter(e => e.value == item.defaultValue)[0]
					if (opt) {
						return opt.label
					} else {
						return ''
					}
				}
			},
			getDate(res) {
				//返回  yyyy-mm-dd hh-mm-ss格式的时间
				const date = new Date(res)
				const year = date.getFullYear()
				const month = date.getMonth() + 1
				const day = date.getDate()
				const hour = date.getHours()
				const minute = date.getMinutes()
				const second = date.getSeconds()
				return `${year}-${month}-${day} ${hour}:${minute}:${second}`
			},
			getStatusText(index) {
				const list = ['待审核', '合格', '不合格']
				return list[index]
			},
			getItem(e, dc = '') {
				const resItem = {}
				resItem.vModel = e.__vModel__
				resItem.tag = e.__config__.tag
				resItem.tagIcon = e.__config__.tagIcon
				resItem.defaultValue = e.__config__.defaultValue
				resItem.label = e.__config__.label
				resItem.required = e.__config__.required
				resItem.showLabel = e.__config__.showLabel
				resItem.showLabel = e.__config__.showLabel
				resItem.slot = e.__slot__
				resItem.span = e.__config__.span
				resItem.titleText = e.titleText
				resItem.action = e.action
				resItem.placeholder = e.placeholder
				resItem.disabled = e.disabled
				if (dc) {
					resItem.displayCondition = dc
				}
				if (e.displayCondition) {
					resItem.displayCondition = dc + ',' + e.displayCondition
				}
				if (resItem.tag == 'el-upload') {
					resItem.defaultValue = e['file-list'].map(item => item.url).join(',')
				}
				if (resItem.tag == 'el-select') {
					resItem.multiple = e.multiple
				}
				return resItem
			},
			getHtmlInfo() {
				this.contentJosn.fields.forEach(item => {
					let resItemArr = []
					if (item.__config__.children) {
						let displayCondition = ''
						if (item.displayCondition) {
							displayCondition = item.displayCondition
						}
						resItemArr = this.forEachChildren(item.__config__.children, displayCondition)
					} else {
						const resItem = this.getItem(item)
						resItemArr.push(resItem)
					}
					this.htmlRes.push(resItemArr)
				})
				console.log(this.htmlRes);
			},
			forEachChildren(children, dc = '') {
				const res = []
				children.forEach(item => {
					let resItem = {}
					if (item.__config__.children) {
						let displayCondition = dc
						if (item.displayCondition) {
							if (displayCondition) {
								displayCondition = displayCondition + ',' + item.displayCondition
							} else {
								displayCondition = item.displayCondition
							}
						}
						const arr = this.forEachChildren(item.__config__.children, displayCondition)
						res.push(...arr)
					} else {
						resItem = this.getItem(item, dc)
						res.push(resItem)
					}
				})
				return res
			},
			isHaveNoChildShow(e) {
				let flag = true
				e.forEach(item => {
					if (this.isShowItem(item)) {
						flag = false
					}
				})
				return flag
			},
			isShowItem(item) {
				let flag = true
				if (item.displayCondition) {
					const dcArr = item.displayCondition.split(',')
					dcArr.forEach(e => {
						const arr = e.split('/==/')
						this.htmlRes.forEach(group => {
							group.forEach(res => {
								if (res.vModel == arr[0]) {
									if (res.defaultValue != arr[1]) {
										flag = false
									}
								}
							})

						})

					})
				}
				return flag
			},
			// 选中任一radio时，由radio-group触发
			radioGroupChange(e, val) {
				console.log(e);
			},
			clickImg(list, i) {
				uni.previewImage({
					urls: list.split(','), //需要预览的图片http链接列表，多张的时候，url直接写在后面就行了
					current: i, // 当前显示图片的http链接，默认是第一个
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.fillOrderWrapper {
		width: 100%;
		background-color: #F5F9FC;
		box-sizing: border-box;
		padding: 0 30rpx;
		padding-top: 50rpx;

		.title {
			width: 100%;
			height: 50rpx;
			position: relative;
			padding-left: 10rpx;
			line-height: 50rpx;
			font-weight: bold;

			&::before {
				content: '';
				width: 10rpx;
				height: 50rpx;
				position: absolute;
				top: 0%;
				left: 0%;
				transform: translateX(-100%);
				background-color: #05c;
			}
		}

		.infoBox {
			width: 100%;
			margin-bottom: 20rpx;
			box-sizing: border-box;
			padding: 0 20rpx;
			background-color: #fff;
			border-radius: 20rpx;
			padding-top: 20rpx;
			padding-bottom: 40rpx;

			.info {
				width: 100%;
				display: flex;
				justify-content: space-between;
				align-items: center;
				height: 80rpx;
				border-bottom: 2rpx solid #ddd;

				.name {
					flex-shrink: 0;
					min-width: 150rpx;
				}

				.value {
					white-space: pre-wrap;
					line-break: anywhere;
				}

				&:last-child {
					border-bottom: 0;
				}
			}
		}

		.itemWRapper {
			background-color: #f4f4f4;
			border-radius: 10rpx;
			width: 100%;
			box-sizing: border-box;
			padding: 15rpx;
		}

		.comItemBox {
			width: 100%;
			box-sizing: border-box;
			padding: 10rpx;
			margin-bottom: 20rpx;
			display: flex;
			flex-wrap: wrap;
			justify-content: space-between;
			align-items: flex-start;
			border-bottom: 2rpx solid #ddd;

			.comItem {
				margin-bottom: 10rpx;
				display: flex;
				align-items: center;
				word-break: break-all;

				.title_label {
					margin-right: 10rpx;
					flex-shrink: 0;
				}

				.row-title {
					line-break: anywhere;
				}

				.el-input {
					width: 100%;
					flex-shrink: 1;
				}
			}

			.file {
				.imgBox {
					display: flex;
					flex-wrap: wrap;

					.img {
						width: 200rpx;
						height: 200rpx;
						margin-right: 15rpx;
					}
				}
			}
		}

		.nochild {
			border: 0;
			margin-bottom: 0;
			padding: 0;
		}
	}
</style>
<style lang="scss">
	.u-radio__icon-wrap--disabled--checked {
		.u-icon__icon {
			color: #05c !important;
		}
	}
</style>